package com.smartao.base.department;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.smartao.base.Constant;
import com.smartao.base.employee.EmployeeService;
import com.smartao.base.position.PositionService;
import com.smartao.base.selectitem.Selectitem;
import com.smartao.base.selectitem.SelectitemService;
import com.smartao.base.selecttype.SelecttypeService;
import com.smartao.core.mvc.BaseAction;

@Controller
@Scope("prototype")
@Results({ @Result(name = "json", type="json", params={"root","departments"}), 
		@Result(name = "jsonSuccess", type="json", params={"root","jsonMap"}),
		@Result(name = "jsonFailure", type="json", params={"root","jsonMap"}),
		@Result(name = "update", location="update.jsp")})
public class DepartmentAction extends BaseAction{
	
	private static final long serialVersionUID = 1343773590916428520L;

	/** Services required by this action **/
	@Autowired
	private DepartmentService departmentService;
	
	@Autowired
	private EmployeeService employeeService;
	
	@Autowired
	private SelectitemService selectitemService;
	
	@Autowired
	private SelecttypeService selecttypeService;
	
	@Autowired
	private PositionService positionService;
	
	/** variables for this action **/
	// 当前操作的组织维度;
	private Selectitem dimension;
	
	// 组织维度列表;
	private List<Selectitem> dimensions;
	
	// 当前操作的部门;
	private Department department;
	
	// 当前操作部门的子部门;
	private List<Department> departments;
	
	private String message;
	
	private Long dimensionId;
	
	private Map<String, Object> jsonMap = new HashMap<String, Object>();
	/** Business methods **/

	@Override
	public String execute() throws Exception {
		dimensions = selectitemService.findBySelecttypeCode(Constant.SELECTTYPE_DEPARTMENT_DIMENSION);
		// 获取当前操作的组织维度;
		// 如果前端没有传组织维度ID,则使用行政维度;
		if(dimensionId==null){
			for(Selectitem selectitem : dimensions){
				if(selectitem.getCode().equals(Constant.SELECTITEM_DEPARTMENT_DIMENTION_ADMIN)){
					dimension = selectitem;
					break;
				}
			}
			department = departmentService.findRoot();
		}
		else{
			for(Selectitem selectitem : dimensions){
				if(selectitem.getId().equals(dimensionId)){
					dimension = selectitem;
					break;
				}
			}
			department = departmentService.findRoot(dimensionId);
		}
		
        return SUCCESS;
    }
	
	public String preUpdate(){
		department = departmentService.findOne(id);
		if(department.getParent()!=null){
			String pathString = departmentService.getPathString(department.getParent());
			department.getParent().setPath(pathString);
		}
		return "update";
	}
	
	public String update(){
		if(department.getParent()!=null && department.getParent().getId()!=null){
			// parent必须手动取一次,因为service在更新的时候有用到parent的leftKey, rightKey属性.
			department.setParent(departmentService.findOne(department.getParent().getId()));
		}
		else{
			department.setParent(null);
		}
		if(department.getDimension()!=null){
			//department.setDimension(selectitemService.get(department.getDimension().getId()));
		}
		departmentService.save(department);
		if(department.getParent()!=null){
			String pathString = departmentService.getPathString(department.getParent());
			department.getParent().setPath(pathString);
		}
		message = "保存成功!";
		return "update";
	}
	
	public String preAdd(){
		department = new Department();
		department.setDimension(selectitemService.findOne(dimensionId));
		Department parent = null;
		if(id==null){
			parent = departmentService.findRoot(dimensionId);
			
		}
		else{
			parent = departmentService.findOne(id);
			
		}
		parent.setPath(departmentService.getPathString(parent));
		department.setParent(parent);
		return "update";
	}
	
	public String delete(){
		Department department = departmentService.findOne(id);
		if(department==null){
			//throw new IllegalStateException("Can not find the department with the id " + id + " you want to delete");
		}
		if(department.getChildrenCount()>0){
			jsonMap.put("success", false);
			jsonMap.put("message", "删除失败. 请先删除下属部门.");
			return "jsonFailure";
		}
		else if(positionService.countByDepartment(id)>0){
			jsonMap.put("success", false);
			jsonMap.put("message", "删除失败. 请先删除下属岗位.");
			return "jsonFailure";
		}
		else if(employeeService.countByDepartment(id)>0){
			jsonMap.put("success", false);
			jsonMap.put("message", "删除失败. 请先删除该部门下的员工.");
			return "jsonFailure";
		}
		departmentService.delete(id);
		jsonMap.put("success", true);
		return "jsonSuccess";
	}
	
	public String findSubDepartment(){
		departments = departmentService.getDirectChild(id);
		return "json";
	}
	
	/** Getter/Setter for action variables **/
	public Selectitem getDimension() {
		return dimension;
	}

	public void setDimension(Selectitem dimension) {
		this.dimension = dimension;
	}

	public List<Selectitem> getDimensions() {
		return dimensions;
	}

	public void setDimensions(List<Selectitem> dimensions) {
		this.dimensions = dimensions;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public List<Department> getDepartments() {
		return departments;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Long getDimensionId() {
		return dimensionId;
	}

	public void setDimensionId(Long dimensionId) {
		this.dimensionId = dimensionId;
	}

	public Map<String, Object> getJsonMap() {
		return jsonMap;
	}

	public void setJsonMap(Map<String, Object> jsonMap) {
		this.jsonMap = jsonMap;
	}
	

}
